﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using RM2012Server.Business;

namespace RM2012Server.Model
{
    public class UserGroupDAO
    {
        DataServices m_UserGroupDAO = new DataServices();

        public DataTable GetAll()
        {
            m_UserGroupDAO.Load("GetAllGroup", null);
            return m_UserGroupDAO;
        }

        public DataTable GetById(String userGroupId)
        {
            SqlParameter param = new SqlParameter("@UserGroupId", SqlDbType.VarChar, 4);
            param.Value = userGroupId;
            m_UserGroupDAO.Load("GetGroupById", param);
            return m_UserGroupDAO;
        }

        public DataTable GetByLikeId(String userGroupId)
        {
            SqlParameter param = new SqlParameter("@UserGroupId", SqlDbType.VarChar, 4);
            param.Value = userGroupId;
            m_UserGroupDAO.Load("GetGroupByLikeId", param);
            return m_UserGroupDAO;
        }

        public DataTable GetByName(String userGroupName)
        {
            SqlParameter param = new SqlParameter("@UserGroupName", SqlDbType.NVarChar, 100);
            param.Value = userGroupName;
            m_UserGroupDAO.Load("GetGroupByName", param);
            return m_UserGroupDAO;
        }

        public int Add(UserGroupInfo info)
        {
            SqlParameter[] param = new SqlParameter[9];
            param[0] = new SqlParameter("@UserGroupId", SqlDbType.VarChar, 4);
            param[0].Value = info.UserGroupId;

            param[1] = new SqlParameter("@UserGroupName", SqlDbType.NVarChar, 100);
            param[1].Value = info.UserGroupName;

            param[2] = new SqlParameter("@IsDefault", SqlDbType.Bit);
            param[2].Value = info.IsDefault;

            param[3] = new SqlParameter("@IsAdmin", SqlDbType.Bit);
            param[3].Value = info.IsAdmin;

            param[4] = new SqlParameter("@IsMod", SqlDbType.Bit);
            param[4].Value = info.IsMod;

            param[5] = new SqlParameter("@CanDelete", SqlDbType.Bit);
            param[5].Value = info.CanDelete;

            param[6] = new SqlParameter("@CanReport", SqlDbType.Bit);
            param[6].Value = info.CanReport;

            param[7] = new SqlParameter("@CanEdit", SqlDbType.Bit);
            param[7].Value = info.CanEdit;

            param[8] = new SqlParameter("@Remark", SqlDbType.NVarChar, 500);
            param[8].Value = info.Remark;

            return m_UserGroupDAO.ExecuteUpdate("AddGroup", param);
        }

        public int Update(UserGroupInfo info)
        {
            SqlParameter[] param = new SqlParameter[9];
            param[0] = new SqlParameter("@UserGroupId", SqlDbType.VarChar, 4);
            param[0].Value = info.UserGroupId;

            param[1] = new SqlParameter("@UserGroupName", SqlDbType.NVarChar, 100);
            param[1].Value = info.UserGroupName;

            param[2] = new SqlParameter("@IsDefault", SqlDbType.Bit);
            param[2].Value = info.IsDefault;

            param[3] = new SqlParameter("@IsAdmin", SqlDbType.Bit);
            param[3].Value = info.IsAdmin;

            param[4] = new SqlParameter("@IsMod", SqlDbType.Bit);
            param[4].Value = info.IsMod;

            param[5] = new SqlParameter("@CanDelete", SqlDbType.Bit);
            param[5].Value = info.CanDelete;

            param[6] = new SqlParameter("@CanReport", SqlDbType.Bit);
            param[6].Value = info.CanReport;

            param[7] = new SqlParameter("@CanEdit", SqlDbType.Bit);
            param[7].Value = info.CanEdit;

            param[8] = new SqlParameter("@Remark", SqlDbType.NVarChar, 500);
            param[8].Value = info.Remark;

            return m_UserGroupDAO.ExecuteUpdate("UpdateGroup", param);
        }

        public int Delete(String userGroupId)
        {
            SqlParameter param = new SqlParameter("@UserGroupId", SqlDbType.VarChar, 4);
            param.Value = userGroupId;

            return m_UserGroupDAO.ExecuteNoneQuery("DeleteGroup", param);
        }

        public DataRow AddNewRow()
        {
            return m_UserGroupDAO.NewRow();
        }

        public void AddNewGroup(DataRow m_Row)
        {
            m_UserGroupDAO.Rows.Add(m_Row);
        }

        public bool SaveData()
        {
            return m_UserGroupDAO.ExecuteNoneQuery() > 0;
        }
    }
}
